//
// (C) Copyright 2009 Irantha Suwandarathna (irantha@gmail.com)
// All rights reserved.
//

/* Copyright (c) 1995-2000, The Hypersonic SQL Group.
 * All rights reserved.
 *
 * Redistribution and use _in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions _in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer _in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of the Hypersonic SQL Group nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE HYPERSONIC SQL GROUP,
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * on behalf of the Hypersonic SQL Group.
 *
 *
 * For work added by the HSQL Development Group:
 *
 * Copyright (c) 2001-2008, The HSQL Development Group
 * All rights reserved.
 *
 * Redistribution and use _in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions _in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer _in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of the HSQL Development Group nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


using System;
using EffiProz.Core.RowIO;
using EffiProz.Core.Lib;
using EffiProz.Core.Indexes;
using EffiProz.Core.Errors;

namespace EffiProz.Core
{

    // fredt@users 20020221 - patch 513005 by sqlbob@users (RMP)
    // fredt@users 20020920 - patch 1.7.1 - refactoring to cut memory footprint
    // fredt@users 20021205 - patch 1.7.2 - enhancements
    // fredt@users 20021215 - doc 1.7.2 - javadoc comments
    // boucherb@users - 20040411 - doc 1.7.2 - javadoc comments

    /**
     *  In-memory representation of a disk-based database row object with  methods
     *  for serialization and de-serialization. <p>
     *
     *  A CachedRow is normally part of a circular double linked list which
     *  contains all of the Rows currently _in the Cache for the database. It is
     *  unlinked from this list when it is freed from the Cache to make way for
     *  other rows.
     *
     *  New class from the Hypersonic Original
     *
     * @author Thomas Mueller (Hypersonic SQL Group)
     * @version    1.7.2
     * @since Hypersonic SQL
     */
    public class RowAVLDisk : RowAVL
    {
        public const int NO_POS = -1;

        //
        protected TableBase table;
        protected int storageSize;
        int keepCount;
        bool _isInMemory;
        int usageCount;

        /**
         *  Flag indicating unwritten data.
         */
        protected bool hasDataChanged;

        /**
         *  Flag indicating Node data has changed.
         */
        bool hasNodesChanged;

        /**
         *  Default constructor used only in subclasses.
         */
        public RowAVLDisk() { }

        /**
         *  Constructor for new Rows.  Variable hasDataChanged is set to true in
         *  order to indicate the data needs saving.
         *
         * @param t table
         * @param o row data
         */
        public RowAVLDisk(TableBase t, Object[] o)
        {

            table = t;
            tableId = table.getId();

            setNewNodes();

            rowData = o;
            hasDataChanged = hasNodesChanged = true;
        }

        /**
         *  Constructor when read from the disk into the Cache.
         *
         * @param t table
         * @param in data source
         * @throws IOException
         */
        public RowAVLDisk(TableBase t, RowInputInterface _in)
        {

            table = t;
            tableId = table.getId();

            position = _in.getPos();
            storageSize = _in.getSize();

            int indexcount = t.getIndexCount();

            nPrimaryNode = new NodeAVLDisk(this, _in, 0);

            NodeAVL n = nPrimaryNode;

            for (int i = 1; i < indexcount; i++)
            {
                n.nNext = new NodeAVLDisk(this, _in, i);
                n = n.nNext;
            }

            rowData = _in.readData(table.getColumnTypes());
        }

        public override NodeAVL insertNode(int index)
        {
            return null;
        }

        private void readRowInfo(RowInputInterface _in)
        {

            // for use when additional transaction info is attached to rows
        }

        /**
         * Sets flag for Node data change.
         */
        public void setNodesChanged()
        {
            lock (this)
            {
                hasNodesChanged = true;
            }
        }

        public override void updateAccessCount(int count)
        {
            usageCount = count;
        }

        public override int getAccessCount()
        {
            return usageCount;
        }

        public override int getStorageSize()
        {
            return storageSize;
        }

        public override bool isMemory()
        {
            return false;
        }

        /**
         * Sets the file position for the row
         *
         * @param pos position in data file
         */
        public override void setPos(int pos)
        {

            position = pos;

            NodeAVL n = nPrimaryNode;

            while (n != null)
            {
                ((NodeAVLDisk)n).iData = position;
                n = n.nNext;
            }
        }

        /**
         * Sets flag for row data change.
         */
        public override void setChanged()
        {
            lock (this)
            {
                hasDataChanged = true;
            }
        }

        /**
         * Returns true if Node data has changed.
         *
         * @return bool
         */
        public override bool hasChanged()
        {
            lock (this)
            {
                return hasNodesChanged || hasDataChanged;
            }
        }

        /**
         * Returns the Table to which this Row belongs.
         *
         * @return Table
         */
        public override TableBase getTable()
        {
            return table;
        }

        public override void setStorageSize(int size)
        {
            storageSize = size;
        }

        /**
         * Returns true if any of the Nodes for this row is a root node.
         * Used only in Cache.java to avoid removing the row from the cache.
         *
         * @return bool
         */
        public override bool isKeepInMemory()
        {
            lock (this)
            {
                return keepCount > 0;
            }
        }

        public override void destroy()
        {

            base.destroy();

            table = null;
        }

        public override bool keepInMemory(bool keep)
        {
            lock (this)
            {
                if (!_isInMemory)
                {
                    return false;
                }

                if (keep)
                {
                    keepCount++;
                }
                else
                {
                    keepCount--;

                    if (keepCount < 0)
                    {
                        throw Error.runtimeError(ErrorCode.U_S0500,
                                                 "RowAVLDisk - keep count");
                    }
                }

                return true;
            }
        }

        public override bool isInMemory()
        {
            lock (this)
            {
                return _isInMemory;
            }
        }

        public override void setInMemory(bool _in)
        {
            lock (this)
            {
                _isInMemory = _in;

                if (_in)
                {
                    return;
                }

                NodeAVL n = nPrimaryNode;

                while (n != null)
                {
                    n.setInMemory(_in);

                    n = n.nNext;
                }

                RowAction action = rowAction;

                if (action != null)
                {
                    action.memoryRow = null;
                }
            }
        }

        /**
         * used in CachedDataRow
         */
        public virtual void setNewNodes()
        {

            int indexcount = table.getIndexCount();

            nPrimaryNode = new NodeAVLDisk(this, 0);

            NodeAVL n = nPrimaryNode;

            for (int i = 1; i < indexcount; i++)
            {
                n.nNext = new NodeAVLDisk(this, i);
                n = n.nNext;
            }
        }

        public override int getRealSize(RowOutputInterface _out)
        {

            int size = _out.getSize((RowAVLDisk)this)
                       + table.getIndexCount() * NodeAVLDisk.SIZE_IN_BYTE;

            return size;
        }

        /**
         * Used exclusively by Cache to save the row to disk. New implementation in
         * 1.7.2 writes out only the Node data if the table row data has not
         * changed. This situation accounts for the majority of invocations as for
         * each row deleted or inserted, the Nodes for several other rows will
         * change.
         */
        public override void write(RowOutputInterface _out)
        {

            try
            {
                writeNodes(_out);

                if (hasDataChanged)
                {
                    _out.writeData(rowData, table.colTypes);
                    _out.writeEnd();

                    hasDataChanged = false;
                }
            }
            catch (System.IO.IOException ) { }
        }

        public override void write(RowOutputInterface _out, IntLookup lookup)
        {

            _out.writeSize(storageSize);

            NodeAVL rownode = nPrimaryNode;

            while (rownode != null)
            {
                ((NodeAVLDisk)rownode).writeTranslate(_out, lookup);

                rownode = rownode.nNext;
            }

            _out.writeData(getData(), table.colTypes);
            _out.writeEnd();
        }

        /**
         *  Writes the Nodes, immediately after the row size.
         *
         * @param out
         *
         * @throws IOException
         */
        private void writeNodes(RowOutputInterface _out)
        {

            _out.writeSize(storageSize);

            NodeAVL n = nPrimaryNode;

            while (n != null)
            {
                n.write(_out);

                n = n.nNext;
            }

            hasNodesChanged = false;
        }

        /**
         * Lifetime scope of this method depends on the operations performed on
         * any cached tables since this row or the parameter were constructed.
         * If only deletes or only inserts have been performed, this method
         * remains valid. Otherwise it can return invalid results.
         *
         * @param obj row to compare
         * @return bool
         */
        public override bool Equals(Object obj)
        {

            if (obj == this)
            {
                return true;
            }

            if (obj is RowAVLDisk)
            {
                return ((RowAVLDisk)obj).position == position;
            }

            return false;
        }

        /**
         * Hash code is valid only until a modification to the cache
         *
         * @return file position of row
         */
        public override int GetHashCode()
        {
            return position;
        }
    }
}
